package com.example.test_witns.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ScrollView;

public class CustomScrollView extends ScrollView {
    private float lastInterceptX = 0;
    private float lastInterceptY = 0;
    private float lastTouchX = 0;

    public CustomScrollView(Context context) {
        super(context);
    }

    public CustomScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        final float x = ev.getX();
        final float y = ev.getY();

        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastInterceptX = x;
                lastInterceptY = y;
                lastTouchX = x;
                return false;
            case MotionEvent.ACTION_MOVE:
                final float deltaX = x - lastInterceptX;
                final float deltaY = y - lastInterceptY;
                if (Math.abs(deltaX) > Math.abs(deltaY)) {
                    // 横向滑动，交给 ViewPager2 处理
                    return true;
                }
                lastTouchX = x;
                break;
        }
        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        final float x = ev.getX();

        switch (ev.getAction()) {
            case MotionEvent.ACTION_MOVE:
                final float dx = lastTouchX - x;
                // 横向滑动，交给 ViewPager2 处理
                if (Math.abs(dx) > 0) {
                    return false;
                }
                break;
        }
        lastTouchX = x;
        return super.onTouchEvent(ev);
    }
}